package com.xoms.admin.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xoms.admin.controller.res.bean.RProPriceBean;
import com.xoms.admin.model.AccountBean;
import com.xoms.admin.model.ProPriceBean;
import com.xoms.admin.persist.dao.ProPriceDao;
import com.xoms.admin.service.ProPriceService;
import com.xoms.admin.service.exception.ServiceException;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.AppTextUtil;
import com.xoms.util.DateTimeUtil;
import com.xoms.util.StringUtil;

@Service
public class ProPriceServiceImpl implements ProPriceService {
	
	Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	ProPriceDao proPriceImpl;

	@Override
	public PageResult<RProPriceBean> getPrices(AccountBean account, String key, String productId, int page,
			int pagesize) {
		PageResult<RProPriceBean> result = new PageResult<RProPriceBean>();
		List<RProPriceBean> resList = new ArrayList<RProPriceBean>();
		// 获取价格数量
		int count = proPriceImpl.selectCount(key, productId);
		if (count > 0) {
			// 数量不为0时获取价格列表
			List<ProPriceBean> lists = proPriceImpl.selectList(key, productId, page, pagesize);

			if (lists != null) {
				RProPriceBean rp = null;
				for (ProPriceBean price : lists) {
					rp = new RProPriceBean();
					rp = parse(price);
					resList.add(rp);
				}
			}
		}
		
		result.setData(resList);
		result.setCount(count);
		return result;
	}
	
	/**
	 * 格式化数据
	 * @param punit
	 * @return
	 */
	private RProPriceBean parse(ProPriceBean price) {
		RProPriceBean res = new RProPriceBean();
		res.setAccount(price.getAccount());
		res.setPid(price.getPid());
		res.setPrice(price.getPrice());
		res.setProductId(price.getProductId());
		res.setStartTime(DateTimeUtil.formatTimestamp(price.getStartTime()));
		res.setEndTime(DateTimeUtil.formatTimestamp(price.getEndTime()));
		res.setStatus(price.getStatus());
		res.setRemark(price.getRemark());
		res.setCreateTime(DateTimeUtil.formatTimestamp(price.getCreateTime()));
		return res;
	}

	@Transactional(rollbackFor = Exception.class)// 事务
	@Override
	public RProPriceBean edit(AccountBean account, String pid, Double price, String productId, String remark,
			String startTime, String endTime, int isEnable) {
		boolean flag = false;
		ProPriceBean data = new ProPriceBean();
		data.setProductId(productId);
		data.setPrice(price);
		data.setStatus(isEnable);
		data.setRemark(remark);
		data.setStartTime(DateTimeUtil.toTimestamp(startTime, "yyyy-MM-dd HH:mm:ss"));
		data.setEndTime(DateTimeUtil.toTimestamp(endTime, "yyyy-MM-dd HH:mm:ss"));
		if (StringUtil.isEmpty(pid)) {// 新建
			// 新建时判断开始时间是不是在最后生效时间之后
			Timestamp lastEndTime = proPriceImpl.selectLastEndTime(productId);
			logger.info("startTime:======" + DateTimeUtil.formatTimestamp(data.getStartTime()));
			if(lastEndTime != null) {
				long lasteTime = DateTimeUtil.formatTimestamp(lastEndTime);
				logger.info("lastEndTime:======" + lasteTime);
				if(DateTimeUtil.formatTimestamp(data.getStartTime()) < lasteTime) {
					throw new ServiceException("pro_price_startTime_error");
				}
			}
			pid = AppTextUtil.getPrimaryKey();// id
			data.setPid(pid);
			data.setAccountId(account.getAccid());
			flag = proPriceImpl.insert(data) > 0;
			if (!flag) {
				throw new ServiceException("database_insert_fail","商品价格");
			}
		} else {
			data.setPid(pid);
			flag = proPriceImpl.update(data) > 0;// 更新数据库
			if (!flag) {
				throw new ServiceException("database_update_fail","商品价格");
			}
		}
		return parse(proPriceImpl.selectOneById(pid));
	
	}

	@Override
	public boolean enable(AccountBean account, String pid) {
		if (proPriceImpl.updateStatus(pid, 1) > 0) {// 更新状态
			return true;
		}

		return false;
	}

	@Override
	public boolean disable(AccountBean account, String pid) {
		// 判读当前商品有无价格
		ProPriceBean price = proPriceImpl.selectOneById(pid);
		if(price == null) {
			throw new ServiceException("object_is_not_exist","商品价格");
		}
		Double p = proPriceImpl.selectProductPrice(price.getProductId());
		if(p == null) {
			throw new ServiceException("object_is_not_exist","商品价格");
		}
		if (proPriceImpl.updateStatus(pid, 2) > 0) {// 更新状态
			return true;
		}

		return false;
	}

	@Transactional(rollbackFor = Exception.class)// 事务
	@Override
	public boolean del(AccountBean account, String pid) {
		// 判读当前商品有无价格
		ProPriceBean price = proPriceImpl.selectOneById(pid);
		if(price == null) {
			throw new ServiceException("object_is_not_exist","商品价格");
		}
		Double p = proPriceImpl.selectProductPrice(price.getProductId());
		if(p == null) {
			throw new ServiceException("object_is_not_exist","商品价格");
		}
		if (proPriceImpl.del(pid) > 0) {// 删除价格
			return true;
		}
		return false;
	}
	
	
}
